from flask_admin.contrib.sqla import ModelView
from flask_login import current_user
from sqlalchemy import text

# Customized Model View
class CustomModelView(ModelView):
    page_size = 10
    can_view_details = True

    # Customized admin interface
    list_template = 'admin/custom/list.html'
    create_template = 'admin/custom/create.html'
    edit_template = 'admin/custom/edit.html'

    # 登录方可显示该Model View，
    def is_accessible(self):
        """
            Override this method to add permission checks.

            Flask-Admin does not make any assumptions about the authentication system used in your application, so it is
            up to you to implement it.

            By default, it will allow access for everyone.
        """
        return current_user.is_authenticated
        # return True

    def get_query(self):
        return super(CustomModelView, self).get_query().order_by(text('-id'))